using UnityEngine;
using System.Collections;

public class Archeologist : MonoBehaviour {

    public delegate void ActionEvent();

    public Texture[] texturesAnims;
    private Const.ACdirection previousDir;
    private Const.ACdirection currentDir;
    private bool canMove = true;
    public AudioClip deathSound;
    private Vector3 targetPosition;
    private Vector2 _tilePosition;
    private float _animation_speed = 1;
    private AnimationCurve[,] mvtCurveArray;
    private float currentFrame = 0;
    private Const.ACstate _currentState;
    public AudioClip clip;

    private GameObject _poufAnim;
    //private 
    private bool _alreadyChangeDir = false;
	// Use this for initialization
	void Start () 
    {
        GameObject pouf = Resources.Load("Prefabs/PoufAnim") as GameObject;
        _poufAnim = Instantiate(pouf) as GameObject;
        _poufAnim.renderer.enabled = false;

        currentDir = Const.ACdirection.RIGHT;
        ApplyNewDir();        
	}

    public void Restart()
    {
        _currentState = Const.ACstate.STAND;
        this.rigidbody.velocity = new Vector3(0, 0, 0);

        Texture tex = texturesAnims[((int)_currentState)];
        this.GetComponent<Anim>().loop = true;
        this.GetComponent<Anim>().speed = 0.1f;
        this.GetComponent<Anim>().resetTimer(tex, 2);

        if(_poufAnim)
            _poufAnim.renderer.enabled = false;
    }
	
	// Update is called once per frame
	void Update () 
    {
        _tilePosition = new Vector2((int)this.transform.position.x,(int)this.transform.position.z);

        
        if (_currentState == Const.ACstate.STAND)
        {
            this.rigidbody.velocity = new Vector3(0, 0, 0);
            Texture tex = texturesAnims[((int)_currentState)];
            this.GetComponent<Anim>().loop = true;
            this.GetComponent<Anim>().speed = 0.1f;
            this.GetComponent<Anim>().resetTimer(tex, 2);
        }
         
	}

    void FixedUpdate()
    {
        _alreadyChangeDir = false;
    }

    void ApplyNewDir()
    {

        float speed = Const._AC_walk_speed;
        switch (currentDir)
        {
            case Const.ACdirection.LEFT:
                this.transform.LookAt(this.transform.position - Vector3.left);
                this.rigidbody.velocity = new Vector3(-speed /* Time.deltaTime*/, 0, 0);
                break;
            case Const.ACdirection.RIGHT:
                this.rigidbody.velocity = new Vector3(speed /* Time.deltaTime*/, 0, 0);
                this.transform.LookAt(this.transform.position - Vector3.right);
                break;
            case Const.ACdirection.DOWN:
                this.transform.LookAt(this.transform.position + Vector3.forward);
                this.rigidbody.velocity = new Vector3(0, 0, -speed /* Time.deltaTime*/);
                break;
            case Const.ACdirection.UP:
                this.transform.LookAt(this.transform.position - Vector3.forward);
                this.rigidbody.velocity = new Vector3(0, 0, speed /* Time.deltaTime*/);
                break;
            case Const.ACdirection.STOP:
                this.rigidbody.velocity = new Vector3(0, 0, 0);
                break;
        }


        if (_currentState == Const.ACstate.DEAD)
            return;

        _currentState = Const.ACstate.WALK;        

        Texture tex = texturesAnims[((int)_currentState)];
        this.GetComponent<Anim>().loop = true;
        this.GetComponent<Anim>().speed = 0.1f;
        this.GetComponent<Anim>().resetTimer(tex, 4);
        

    }

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.layer == Const.LAYER_GROUND || _alreadyChangeDir)
            return;
        switch (currentDir)
        {
            case Const.ACdirection.RIGHT:
                currentDir = Const.ACdirection.LEFT;
                break;
            case Const.ACdirection.LEFT:
                currentDir = Const.ACdirection.RIGHT;
                break;
            case Const.ACdirection.UP:
                currentDir = Const.ACdirection.DOWN;
                break;
            case Const.ACdirection.DOWN:
                currentDir = Const.ACdirection.UP;
                break;
        }

        //End of run
        var tileCompo = col.gameObject.GetComponent<Tile>();
        if (null != tileCompo)
        {
            if (Const.TYPE_TILE.END == tileCompo.type)
            {
                LevelManager.Instance.EndStage(true);
            }
        }
        _alreadyChangeDir = true;
        ApplyNewDir();
        //this.rigidbody.velocity = new Vector3(0, 0, 0);

        if (_currentState == Const.ACstate.DEAD)
            return;
        /*
        _currentState = Const.ACstate.STAND;
        Texture tex = texturesAnims[((int)_currentState)];
        this.GetComponent<Anim>().speed = 0.3f;
        this.GetComponent<Anim>().resetTimer(tex, 2);
         * */
    }

    public void suicideMe()
    {
        _currentState = Const.ACstate.DEAD;
		 CurrentDir = Const.ACdirection.STOP; 		 AudioSource.PlayClipAtPoint(deathSound, this.gameObject.transform.position);

        //this.renderer.enabled = false;
        Texture tex = texturesAnims[((int)_currentState)];
        this.GetComponent<Anim>().speed = 0.1f;
        this.GetComponent<Anim>().loop = false;
        this.GetComponent<Anim>().resetTimer(tex, 2);
 		AudioSource.PlayClipAtPoint(clip, this.transform.position);        
        this.GetComponent<MovementHandler>().enabled = false;

        _poufAnim.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + 0.5f, this.transform.position.z);
        _poufAnim.GetComponent<Anim>().enabled = true;
        _poufAnim.GetComponent<Anim>().speed = 0.1f;
        _poufAnim.GetComponent<Anim>().nbSprite = 4;
        _poufAnim.GetComponent<Anim>().loop = false;
        _poufAnim.GetComponent<Anim>().Apply();
        _poufAnim.renderer.enabled = true;

        this.GetComponent<MovementHandler>().enabled = false;

        LevelManager.Instance.EndStage(false);

        
    }

    private void constantMove()
    {
        currentFrame += Time.deltaTime;
        
        if (currentDir != previousDir && currentFrame >  _animation_speed)
        {
            Debug.Log("ZE VEUT TOURNER");
            float tmpTime = currentFrame - _animation_speed;
            float moveX = mvtCurveArray[(int)previousDir, 0].Evaluate(_animation_speed);
            float moveY = mvtCurveArray[(int)previousDir, 1].Evaluate(_animation_speed);
            this.transform.position = new Vector3((int)(this.transform.position.x + moveX), this.transform.position.y, (int)(this.transform.position.z + moveY));

            moveX = mvtCurveArray[(int)currentDir, 0].Evaluate(tmpTime);
            moveY = mvtCurveArray[(int)currentDir, 1].Evaluate(tmpTime);
            this.transform.position = new Vector3(this.transform.position.x + moveX, this.transform.position.y, this.transform.position.z + moveY);

            //previousDir = currentDir;     
            currentDir = previousDir; 
        }
         
        else
        {

            float moveX = (float)mvtCurveArray[(int)currentDir, 0].Evaluate(currentFrame);
            float moveY = (float)mvtCurveArray[(int)currentDir, 1].Evaluate(currentFrame);

            if (currentFrame > _animation_speed)
                currentFrame = currentFrame - _animation_speed;

            //Debug.Log(mvtCurveArray[(int)currentDir, 0].Evaluate(currentFrame);
            //Debug.Log(currentFrame);
            this.transform.position = new Vector3((int)this.transform.position.x + moveX, this.transform.position.y, (int)this.transform.position.z + moveY);
            //previousDir = currentDir;  
        }
              
    }

    public UnityEngine.Vector2 TilePosition
    {
        get { return _tilePosition; }
        set { _tilePosition = value; }
    }
    
    public Const.ACdirection CurrentDir
    {
        get { return currentDir; }
        set { currentDir = value; ApplyNewDir(); }
    }
    
    public Const.ACstate CurrentState
    {
        get { return _currentState; }
        set { _currentState = value; }
    }
}
